home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_7.lha / 5_7 / 5_7a2.c < prev    next >
Text File  |  1993-08-08  |  1KB  |  59 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Add one to the bin which contains a.
  6. / Adjust the histogram, if necessary.
  7. include <histogram.h>
  8. include <string.h>
  9.  
  10. oid histogram::add(int a)
  11.  
  12.    register int i, j;
  13.  
  14.    // make l <= a < r, possibly expanding histogram by
  15.    // doubling the binsize and range.
  16.    // First the left end.
  17.    while (a < l)
  18. {
  19. // combine the bins
  20. for (i = nbin - 1, j = nbin - 2;
  21.      0 <= j;
  22.      i--, j -= 2)
  23.     h[i] = h[j] + h[j + 1];
  24.  
  25. // check for odd number of bins
  26. if (j == -1)
  27.     h[i--] = h[0];
  28.  
  29. // zero out the rest
  30. if (i >= 0)
  31.     memset(h, 0, (i + 1) * sizeof(int));
  32. binsize += binsize;
  33. l -= r - l;
  34. }
  35.  
  36.    // Then the right end
  37.    while (r <= a)
  38. {
  39. // combine the bins
  40. int halfbin = nbin / 2;
  41. for (i = 0, j = 0; i < halfbin; i++, j += 2)
  42.     h[i] = h[j] + h[j + 1];
  43.  
  44. // check for odd number of bins
  45. if (j < nbin)
  46.     h[i++] = h[j];
  47.  
  48. // zero out the rest
  49. if (i < nbin)
  50.     memset(&h[i], 0, (nbin - i) * sizeof(int));
  51. binsize += binsize;
  52. r += r - l;
  53. }
  54.  
  55.    sum += a;
  56.    sqsum += a * a;
  57.    h[(a - l) / binsize]++;
  58.  
  59.